﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Win32;

using VicMessenger.DB;
using VicMessenger.BLL;
using System.Data;
using System.Reflection;
using VicMessenger.UserControls;
using SBLib.Common.DbKS;
using SBLib.Common.SqlServerKS;
using VicMessenger.Common;
using SBLib.Common.CsvKS;

namespace VicMessenger
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    /// 
    public partial class MainWindow : Window
    {

        DataTable globalDt;

        DatabaseFuncKS dbfunc;
        SqlServerFuncKS sqlfunc; 

        public MainWindow()
        {
            InitializeComponent();

            Loaded += new RoutedEventHandler(MainWindow_Loaded);

            
        }

        void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {



            CommonCls.GlobalDestinationConnectionString = CommonCls.BuilderSqlConnectionString(true, @"BILAI-PC\SQLEXPRESS", "VicMessenger", "", "");
            CommonCls.GlobalSourceConnectioinString = CommonCls.BuilderSqlConnectionString(true, @"BILAI-PC\SQLEXPRESS", "MyCMS", "", "");

            dbfunc = new DatabaseFuncKS(CommonCls.GlobalDestinationConnectionString);

            sqlfunc = new SqlServerFuncKS(CommonCls.GlobalDestinationConnectionString);

            PagingCtl.FirstButton += new EventHandler<EventArgs>(PagingCtl_FirstButton);
            PagingCtl.LastButton += new EventHandler<EventArgs>(PagingCtl_LastButton);
            PagingCtl.PrevButton += new EventHandler<EventArgs>(PagingCtl_PrevButton);
            PagingCtl.NextButton += new EventHandler<EventArgs>(PagingCtl_NextButton);
        }

        void PagingCtl_NextButton(object sender, EventArgs e)
        {
            //int? prmTotal;
            //globalDt = ContactBLL.GetHorizontalContactCustomPaging(1, PagingCtl.CurrentPosition, 1, out prmTotal);
            //lstContact.View = CreateGridViewColumns(globalDt);
            //lstContact.ItemsSource = globalDt.DefaultView;

            //PagingCtl.TotalPages = (int)prmTotal;
        }

        void PagingCtl_PrevButton(object sender, EventArgs e)
        {
           
        }

        void PagingCtl_LastButton(object sender, EventArgs e)
        {
           
        }

        void PagingCtl_FirstButton(object sender, EventArgs e)
        {
        
        }



        //public static GridView CreateGridViewColumns(DataTable dt)
        //{
        //    // Create the GridView
        //    GridView gv = new GridView();
        //    gv.AllowsColumnReorder = true;

        //    // Create the GridView Columns
        //    foreach (DataColumn item in dt.Columns)
        //    {
        //        GridViewColumn gvc = new GridViewColumn();
        //        gvc.Header = item.ColumnName;

        //        FrameworkElementFactory txt = new FrameworkElementFactory(typeof(TextBox));
        //        txt.SetValue(TextBox.MinWidthProperty, (double)200);
        //        Binding bnd = new Binding(item.ColumnName);
        //        bnd.Mode = BindingMode.TwoWay;
        //        txt.SetBinding(TextBox.TextProperty, bnd); // sets binding

        //        // add textbox template
        //        gvc.CellTemplate = new DataTemplate();
        //        gvc.CellTemplate.VisualTree = txt;
        //        gv.Columns.Add(gvc);
        //    }

        //    return gv;
        //}


        //public static GridView CreateGridViewColumns<T>(List<proc_Contact_CustomPagingResult> dt)
        //{
        //    // Create the GridView
        //    GridView gv = new GridView();
        //    gv.AllowsColumnReorder = true;

           

        //    foreach (PropertyInfo p in typeof(T).GetProperties())
        //    {
        //        GridViewColumn gvc = new GridViewColumn();
        //        gvc.Header = p.Name;

        //        FrameworkElementFactory txt = new FrameworkElementFactory(typeof(TextBox));
        //        txt.SetValue(TextBox.MinWidthProperty, (double)180);
        //        Binding bnd = new Binding(p.Name);
        //        bnd.Mode = BindingMode.TwoWay;
        //        txt.SetBinding(TextBox.TextProperty, bnd); // sets binding

               

        //        if (p.Name == "CampaignID" || 
        //            p.Name == "ContactID"  ||
        //            p.Name == "LastRun" ||
        //            p.Name == "RowRank" ||
        //            p.Name == "Status")
        //        {
        //            gvc.Width = 0;
        //        }

        //        // add textbox template
        //        gvc.CellTemplate = new DataTemplate();
        //        gvc.CellTemplate.VisualTree = txt;
        //        gv.Columns.Add(gvc);


        //    }


        //    return gv;
        //}

        //private static void ProcessContact(DataTable prmTable)
        //{

        //    //colsrc.Source = ret;
        //    if (prmTable != null)
        //    {
        //        foreach (DataRow item in prmTable.Rows)
        //        {
        //            proc_Contact_CustomPagingResult modSingleContact = new proc_Contact_CustomPagingResult();
        //            modSingleContact.ContactID = 0;
        //            modSingleContact.CampaignID = 1;
        //            try
        //            {
        //                modSingleContact.Email = item["Email"].ToString().Trim();
        //            }
        //            catch (Exception)
        //            {

        //                modSingleContact.Email = "";
        //            }

        //            //try
        //            //{
        //            //    modSingleContact.FirstName = item["FirstName"].ToString().Trim();
        //            //}
        //            //catch (Exception)
        //            //{

        //            //    modSingleContact.FirstName = "";
        //            //}

        //            //try
        //            //{
        //            //    modSingleContact.MiddleName = item["MiddleName"].ToString().Trim();
        //            //}
        //            //catch (Exception)
        //            //{

        //            //    modSingleContact.MiddleName = "";
        //            //}

        //            //try
        //            //{
        //            //    modSingleContact.LastName = item["LastName"].ToString().Trim();
        //            //}
        //            //catch (Exception)
        //            //{

        //            //    modSingleContact.LastName = "";
        //            //}

        //            modSingleContact.Status = "Pending";

        //            long? retContactID = ContactBLL.InsertContact(modSingleContact);

        //            ProcessCustomField(item, retContactID);

        //            //custom field

        //        }



        //    }
        //}
        //private static void ProcessCustomField(DataRow item, long? retContactID)
        //{
        //    if (retContactID != null)
        //    {
        //        if (retContactID > 0)
        //        {
        //            for (int i = 1; i > -1; i++)
        //            {
        //                try
        //                {
        //                    proc_CustomeField_SelectResult modField = new proc_CustomeField_SelectResult();
        //                    modField.CustomFieldID = 0;
        //                    modField.ContactID = retContactID;
        //                    modField.FieldName = string.Format("viccustomfield{0}", i);
        //                    modField.FieldValue = item[string.Format("viccustomfield{0}", i)].ToString().Trim();

        //                    CustomFieldBLL.InsertUpdateCustomField(modField);
        //                }
        //                catch (Exception)
        //                {
        //                    break;
        //                }
        //            }

        //        }
        //    }
        //}

        private void btnNext_Click(object sender, RoutedEventArgs e)
        {
            ////DataView dv = lstContact.ItemsSource as DataView;

            ////var retDataTable = dv.ToTable();


            
            //DataTable ddd1 = globalDt.GetChanges(DataRowState.Modified);
            //globalDt.AcceptChanges();
            ////globalDt.AcceptChanges();

            //if (ddd1 != null && ddd1.Rows.Count > 0)
            //{
            //    ProcessContact(ddd1);
            //}


            ////foreach (proc_Contact_CustomPagingResult item in lstContact.Items)
            ////{
            ////    ContactBLL.InsertContact(item);
            ////}


            ////MessageBox.Show("Done");
        }

        private void btnDelete_Click(object sender, RoutedEventArgs e)
        {
            //foreach (var item in lstContact.SelectedItems)
            //{
            //    var ret = item as DataRowView;

            //    long _id;
            //    long.TryParse(ret["ContactID"].ToString(), out  _id);
            //    ContactBLL.DeleteContact(_id);



            //}

            //globalDt = null;
            //int? prmTotal;
            //globalDt = ContactBLL.GetHorizontalContactCustomPaging(1, 0, 10, out prmTotal);
            //lstContact.View = CreateGridViewColumns(globalDt);
            //lstContact.ItemsSource = globalDt.DefaultView;

        }

        private void buttonImport_Click(object sender, RoutedEventArgs e)
        {

            CommonCls.GlobalSourceSqlStatement = "select  10 as CampaignID ,* from dbo.Content";

            string tst = "TestCat_TestCam_Contact";

            //file source
            List<string> st = new List<string>();
            //st.Add("Email");
            //st.Add("First Name");
            //st.Add("Middle Name");
            //st.Add("Last Name");
            //st.Add("viccustomfield1");
            //st.Add("viccustomfield2");
            //st.Add("viccustomfield3");


            st.Add("ContentID");
            st.Add("ParentContentID");
            st.Add("TemplateID");
            st.Add("ContentName");
            st.Add("SortOrder");
            st.Add("Active");
            st.Add("ApplicationID");
            st.Add("DateCreated");
            st.Add("DateModified");
            st.Add("CreatedByUserID");
            st.Add("ApprovedByUserID");
            st.Add("Tag");
            st.Add("Published");
            st.Add("CustomXPath");



            if (sqlfunc.IsTableExists("VicMessenger", tst) == false)
            {
                dbfunc.CreateDatabaseTable("VicMessenger", tst, "ContactID", "CampaignID", st);
            }

            List<string> st1 = new List<string>();
            st1.Add("CampaignID");
            st1.Add("ContentID");
            st1.Add("ParentContentID");
            st1.Add("TemplateID");
            st1.Add("ContentName");
            st1.Add("SortOrder");
            st1.Add("Active");
            st1.Add("ApplicationID");
            st1.Add("DateCreated");
            st1.Add("DateModified");
            st1.Add("CreatedByUserID");
            st1.Add("ApprovedByUserID");
            st1.Add("Tag");
            st1.Add("Published");
            st1.Add("CustomXPath");


            if (CommonCls.GlobalSourceSqlStatement.Trim().Length > 0)
            {
                //sql
                dbfunc.BulkInsert(CommonCls.GlobalSourceConnectioinString, CommonCls.GlobalDestinationConnectionString, tst, CommonCls.GlobalSourceSqlStatement, st1);

            }
            else
            {
                CommonCls.GlobalSelectedFileSource = @"C:\Users\Bilai\Desktop\contact.csv";


                globalDt = CsvReadFuncKS.CsvToDataTable(CommonCls.GlobalSelectedFileSource, true, "ContactID", typeof(long), 0, "CampaignID", typeof(int), 11);

                if (globalDt != null)
                {
                    dbfunc.BulkInsert(globalDt, tst);
                }
            }
           
        }
    }
}
